package com.example.shorg.controller;

import com.example.shorg.dto.OrderDTO;
import com.example.shorg.enums.ResultEnum;
import com.example.shorg.exception.ShorgException;
import com.example.shorg.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/***
 * @author hmh
 * @date 2018/12.16
 * 卖家端订单
 * */
@RestController
@RequestMapping("/shorg")
@Slf4j
public class ShorgOrderController {

    @Autowired
    private OrderService orderService;
    @RequestMapping("/heihei")
    public String getList(){
        return "哈哈哈哈哈哈";
    }

    @RequestMapping("/order/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map){
        PageRequest pageRequest = new PageRequest(page-1, size);
        Page<OrderDTO> all = orderService.findAll(pageRequest);
        map.put("orderDtoPage", all);
        map.put("currentPage", page);
        map.put("size", size);

//        all.getTotalPages()
        return new ModelAndView("order/list", map);
    }

    /**
     *取消订单
     **/
    @GetMapping("/order/cancle")
    public ModelAndView cancleOrder(@RequestParam(value = "orderId") String orderId,
                                    Map<String, Object> map){
        try {
            OrderDTO orderDTO = orderService.findOne(orderId);

            orderService.cancle(orderDTO);
        }catch (ShorgException e){
            log.error("【卖家端取消订单】查询不到订单");
            map.put("msg", e.getMessage());
            map.put("url", "/shorg/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_CANCLE_SUCCESS.getMessage());
        map.put("url", "/shorg/order/list");

        return new ModelAndView("common/success", map);
    }

    /**
     * 完成订单
     * */
    @GetMapping("/order/finish")
    public ModelAndView finish(@RequestParam(value = "orderId") String orderId, Map<String, Object> map){
        try {
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }catch (ShorgException e){
            log.error("【卖家端完成订单】 查询不到订单");
            map.put("msg", e.getMessage());
            map.put("url", "/shorg/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_UPDATE_FINISH.getMessage());
        map.put("url", "/shorg/order/list");
        return new ModelAndView("common/success", map);


    }

    /**
     * 查看订单详情
     * */
    @GetMapping("/order/detail")
    public ModelAndView getOrderDetail(@RequestParam(value = "orderId") String orderId, Map<String, Object> map){

        OrderDTO orderDTO = new OrderDTO();
        try{
            orderDTO = orderService.findOne(orderId);
        }catch (ShorgException e){
            log.error("【卖家端查询订单详情】发生异常", e.getMessage());
            map.put("msg", e.getMessage());
            map.put("url","/shorg/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail", map);
    }
}
